package com.juzipi.upload.controller;

import com.juzipi.demo.tools.json.ResultTool;
import com.juzipi.demo.utils.json.Result;
import com.juzipi.upload.service.UploadService;
import com.juzipi.upload.utils.FileUtils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.exception.CosClientException;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @Author juzipi
 * @Date 2021/5/29 16:44
 * @Info
 */

@RestController
@RequestMapping("upload")
public class TCCloudController {


    @Autowired
    private UploadService uploadService;

    @Value("${tc.cos.secretId}")
    private String secretId;

    @Value("${tc.cos.secretKey}")
    private String secretKey;

    //地域
    @Value("${tc.cos.bucket}")
    private String bucket;

    //桶名字
    @Value("${tc.cos.bucketName}")
    private String bucketName;

    //前缀
    @Value("${tc.cos.prefix}")
    private String prefix;

    //本地路径
    @Value("${file.localPath}")
    private String path;


    @PostMapping("upload")
    public Result upload(@RequestParam("file")MultipartFile file) {
        if (file.isEmpty()) {
            return ResultTool.fail();
        }
        //生成的新文件名
        String newFileName = FileUtils.getFileName(file);
        //保存到本地的文件路径
        File dest = FileUtils.saveLocal(file, path);

        if (uploadService.insertFilePath() != 0) {
            //域信息+文件名+本地文件
            if (!this.uploadCloud(secretId,secretKey,bucket,newFileName,dest)) {
                return ResultTool.failData("上传云失败");
            }
            return ResultTool.successData(prefix+newFileName);
        }
        return ResultTool.failData("保存失败");
    }


    @PostMapping("uploadFile")
    public Result uploadFile(@RequestParam("file")MultipartFile file) {
        if (file.isEmpty()) {
            return ResultTool.fail();
        }
        //生成的新文件名
        String newFileName = FileUtils.getFileName(file);
        //保存到本地的文件路径
//        File dest = FileUtils.saveLocal(file, path);
        File dest = FileUtils.conversion(file);
        if (uploadService.insertFilePath() != 0) {
            //域信息+文件名+本地文件
            if (!this.uploadCloud(secretId,secretKey,bucket,newFileName,dest)) {
                return ResultTool.failData("上传云失败");
            }
            return ResultTool.successData(prefix+newFileName);
        }
        FileUtils.deleteTempFile(dest);
        return ResultTool.failData("保存失败");
    }





    /**
     *
     * @param secretId ...
     * @param secretKey ...
     * @param bucket 地域
     * @param newFileName 文件名
     * @param file 文件
     * @return true/false
     */
    private Boolean uploadCloud(String secretId,String secretKey,String bucket,String newFileName,File file){
        try {
            //上传cloud
            //初始化用户身份信息
            BasicCOSCredentials basicCOSCredentials = new BasicCOSCredentials(secretId, secretKey);
            //设置bucket区域
            ClientConfig clientConfig = new ClientConfig(new Region(bucket));
            //生成cos客户端
            COSClient cosClient = new COSClient(basicCOSCredentials, clientConfig);
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, newFileName, file);
            //这个是具体的保存？
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            System.out.println(putObjectResult);
            //返回前缀加文件名就是整个路径了
            cosClient.shutdown();
            return true;
        } catch (CosClientException e) {
            e.printStackTrace();
            return false;
        }
    }

}
